package com.yunhe.transfer.controller.base;

import com.yunhe.common.model.base.DataTransferDTO;
import com.yunhe.common.util.CollectionUtil;
import com.yunhe.transfer.service.base.DataTransferService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: DataTransferController
 * @Author Chen Lun
 * @Date: 2021/1/20
 */
@RestController
@RequestMapping("/measurement")
public class DataTransferController {
    @Autowired
    private DataTransferService dataTransferService;

    /**
     * 接收数据API
     * @param transferDTO
     * @return
     * @throws Exception
     */
    @ApiOperation("接收数据API")
    @RequestMapping(value = "/realtime", method = RequestMethod.POST)
    public ResponseEntity realtimeMeasurement(@RequestBody DataTransferDTO transferDTO) throws Exception {
        if (CollectionUtil.isNotEmpty(transferDTO.getMeasurements()) && transferDTO.getStationCode() != null) {
            dataTransferService.receiveData(transferDTO.getMeasurements(), transferDTO.getStationCode());
        }
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /**
     * 云端补录API
     * @param transferDTO
     * @return
     * @throws Exception
     */
    @ApiOperation("云端补录API")
    @RequestMapping(value = "/recollect", method = RequestMethod.POST)
    public ResponseEntity collectMeasurement(@RequestBody DataTransferDTO transferDTO) throws Exception {
        dataTransferService.recollectData(transferDTO);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /**
     * MQTT主动补招API
     * @param stationId
     * @param dtime
     * @return
     * @throws Exception
     */
    @ApiOperation("MQTT主动补招API")
    @RequestMapping(value = "/collect/mqtt", method = RequestMethod.POST)
    public ResponseEntity collectMeasurementMqtt(@RequestParam Long stationId, 
                                                 @RequestParam List<String> dtime) throws Exception {
        dataTransferService.recollectDataForMqtt(stationId, dtime);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }

    /**
     * WebSocket主动补招API
     * @param stationId
     * @param dtime
     * @return
     * @throws Exception
     */
    @ApiOperation("WebSocket主动补招API")
    @RequestMapping(value = "/collect/ws", method = RequestMethod.POST)
    public ResponseEntity collectMeasurementSocket(@RequestParam Long stationId,
                                                   @RequestParam List<String> dtime) throws Exception {
        dataTransferService.recollectDataForWebSocket(stationId, dtime);
        return new ResponseEntity<>(null, HttpStatus.OK);
    }
}
